﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using GMAC.ReportingSystem.Business.Entities;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Configuration;
using Components = GMAC.ReportingSystem.Business.Components;
using Entities = GMAC.ReportingSystem.Business.Entities;

namespace GMAC.ReportingSystem.Paginas
{
    public partial class Audit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Se verifica que el usuario haya iniciado sesión
            if (!User.Identity.IsAuthenticated || Session["User"] == null)
            {
                System.Web.Security.FormsAuthentication.RedirectToLoginPage();
                return;
            }
            if (IsPostBack) return;
            Initialize();
            ValidateSecurity();

           
        }
        public void Initialize()
        {
            if (!this.IsPostBack)
            {
                var User = new Components.UserComponent();
                ddlUserName.DataSource = User.GetList();
                ddlUserName.DataTextField = "Login";
                ddlUserName.DataValueField = "UserID";
                ddlUserName.DataBind();
                this.ddlUserName.Items.Insert(0, new ListItem("--All--", string.Empty));
                
                var beSecurity = new Components.SecurityComponent();
                var entities = beSecurity.GetEntities();
                this.ddlEntity.DataSource = entities;
                this.ddlEntity.DataTextField = "DisplayName";
                this.ddlEntity.DataValueField = "EntityName";
                this.ddlEntity.DataBind();
                this.ddlEntity.Items.Insert(0, new ListItem("--All--", string.Empty));
            }
        }
        private void ValidateSecurity()
        {
            var beEntities = ((User)Session["User"]).Role.Security.GetEntitySecurity("Audit");
            if (beEntities == null) return;
            if (!beEntities.CanRead)
            {
                pnlSearch.Visible = false;
                rptwReportes.Visible = false;
            }

        }

        protected void imgBuscarReg_Click(object sender, ImageClickEventArgs e)
        {
            List<ReportParameter> parametros = new List<ReportParameter>();
            string StartDate = null;
            if (string.IsNullOrEmpty(this.txtStartDate.Text))
            {
                parametros.Add(new ReportParameter("AuditStartDate", StartDate));
            }
            else
            {
                parametros.Add(new ReportParameter("AuditStartDate", this.txtStartDate.Text));
            }
            string EndDate = null;
            if (string.IsNullOrEmpty(this.txtEndDate.Text))
            {
                parametros.Add(new ReportParameter("AuditEndDate", EndDate));
            }
            else
            {
                parametros.Add(new ReportParameter("AuditEndDate", this.txtEndDate.Text));
            }
            if (String.IsNullOrEmpty(this.ddlUserName.SelectedValue))
            {
                parametros.Add(new ReportParameter("UserId", "0"));
            }
            else
            {
                parametros.Add(new ReportParameter("UserId", this.ddlUserName.SelectedValue));
            }

            if (String.IsNullOrEmpty(this.ddlEntity.SelectedValue))
            {
                parametros.Add(new ReportParameter("AuditName", "0"));
            }
            else
            {
                parametros.Add(new ReportParameter("AuditName", this.ddlEntity.SelectedValue));
            }

            parametros.Add(new ReportParameter("AuditEvent", "0"));



            this.rptwReportes.Style.Add("margin-bottom", "26px");
            this.rptwReportes.ServerReport.ReportPath = ConfigurationManager.AppSettings["FOLDER_REPORT_SERVER"].ToString() + "/ReportAudit";

            this.rptwReportes.ProcessingMode = ProcessingMode.Remote;
            this.rptwReportes.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["REPORT_SERVER"].ToString());
            this.rptwReportes.ServerReport.SetParameters(parametros);
            
        }

        

        


    }
}
